package com.ruoyi.web.controller.construction;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.construction.QualifiPicInfo;
import com.ruoyi.system.service.construction.QualifiPicInfoService;
import com.ruoyi.system.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/qualification/info")
public class QualifiPicInfoController extends BaseController
{

    @Autowired
    private QualifiPicInfoService qualifiPicInfoService;

    /**
     * 查询施工队图片信息列表
     */
    @PreAuthorize("@ss.hasPermi('qualification:info:listpic')")
    @GetMapping("/infopic/list")
    public TableDataInfo list(QualifiPicInfo qualifiPicInfo)
    {
        startPage();
        List<QualifiPicInfo> list = qualifiPicInfoService.selectQualifiPicInfoList(qualifiPicInfo);
        return getDataTable(list);
    }

    /**
     * 导出施工队图片信息列表
     */
    @PreAuthorize("@ss.hasPermi('qualification:info:exportpic')")
    @Log(title = "荣耀资质图片信息", businessType = BusinessType.EXPORT)
    @GetMapping("/infopic/export")
    public AjaxResult export(QualifiPicInfo qualifiPicInfo)
    {
        List<QualifiPicInfo> list = qualifiPicInfoService.selectQualifiPicInfoList(qualifiPicInfo);
        ExcelUtil<QualifiPicInfo> util = new ExcelUtil<QualifiPicInfo>(QualifiPicInfo.class);
        return util.exportExcel(list, "info");
    }

    /**
     * 获取施工队图片信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('qualification:info:querypic')")
    @GetMapping(value = "/infopic/{qualifiPicId}")
    public AjaxResult getInfo(@PathVariable("qualifiPicId") Integer qualifiPicId)
    {
        return AjaxResult.success(qualifiPicInfoService.selectQualifiPicInfoById(qualifiPicId));
    }

    /**
     * 新增施工队图片信息
     */
    @PreAuthorize("@ss.hasPermi('qualification:info:addpic')")
    @Log(title = "荣耀资质图片信息", businessType = BusinessType.INSERT)
    @PostMapping(value = "/infopic/add")
     @ResponseBody


    public AjaxResult add(@RequestParam("file") MultipartFile file,@RequestParam("id") int id,@RequestParam("type") int type) throws Exception
    {
        System.out.println("*******************id*********"+id);
        System.out.println("*******************type*********"+type);
        QualifiPicInfo qualifiPicInfo = new QualifiPicInfo();
        qualifiPicInfo.setConstructionId(id);
        qualifiPicInfo.setModifiedTime(new Date());
        qualifiPicInfo.setQualifiType(type);
        String filePath = RuoYiConfig.getUploadPath();
        String fileName = FileUploadUtils.upload(filePath, file);
        qualifiPicInfo.setConUrl("http://51jhf.com"+fileName);
        System.out.println("*****************图片路径***********"+fileName);
        return toAjax(qualifiPicInfoService.insertQualifiPicInfo(qualifiPicInfo));
    }

    /**
     * 修改施工队图片信息
     */
    @PreAuthorize("@ss.hasPermi('qualification:info:editpic')")
    @Log(title = "荣耀资质图片信息", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/infopic/edit")
    public AjaxResult edit(@RequestBody QualifiPicInfo qualifiPicInfo)
    {
        return toAjax(qualifiPicInfoService.updateQualifiPicInfo(qualifiPicInfo));
    }

    /**
     * 删除施工队图片信息
     */
    @PreAuthorize("@ss.hasPermi('qualification:info:removepic')")
    @Log(title = "施工队图片信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/infopic/{qualifiPicIds}")
    public AjaxResult remove(@PathVariable Integer[] qualifiPicIds)
    {
        return toAjax(qualifiPicInfoService.deleteQualifiPicInfoByIds(qualifiPicIds));
    }

    /**
     * 上传资质图片
     * @param file
     * @return
     * @throws IOException
     */
//    @Log(title = "施工队资质图片信息")
//    @PostMapping("/uploadImage")
//    public AjaxResult uploadImage(@RequestParam("rentalFile") MultipartFile file) throws IOException
//    {
//        if (!file.isEmpty()){
//            String avatar = FileUploadUtils.upload(RuoYiConfig.getQualificationsPath(), file);
//            return AjaxResult.success("上传成功",avatar);
//        }
//        return AjaxResult.error("上传图片异常，请联系管理员");
//    }

//    @PostMapping("/uploadImage")
//    public Map<String,Object> up(MultipartFile file){
//        String url = "/home/ruoyi/profile/xiazai";
//        return FileUtil.uploadImage(file,url);
//    }


}
